import json

from curl_cffi import requests
from curl_cffi.requests.impersonate import BrowserType
from fastapi import APIRouter
from fastapi.responses import HTMLResponse
from pydantic import BaseModel

from db.redis_client import sync_cli
from standard_response import StandardResponseItem

STOCKS_NAMES_REF = {}

router = APIRouter(prefix='/ebooks/api')


class SiLuKeInfo(BaseModel):
    headers: dict


@router.post("/siluke/set_headers")
async def set_headers(info: SiLuKeInfo):
    sync_cli.set('siluke_headers', json.dumps(info.headers))
    return StandardResponseItem()


@router.get("/siluke/search")
async def search(key):
    headers = sync_cli.get("siluke_headers")
    url = "https://www.siluke.com/search.php"
    payload = {"keyword": key}
    resp = requests.post(url, data=payload, headers=json.loads(headers), impersonate=BrowserType.chrome120)
    return HTMLResponse(resp.text, )
